package org.testis.webautomationmodule.ui;

import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;


/**
 * The <code>UIObjectCreator</code> class defines a factory class for creating UIObjects
 * @author nurhat
 */
public class UIObjectCreator
{

    /**
     * A function outputs an UIObject by using reflection
     * @param objType Type of UIObject to be created
     * <ul>
     *    <li>"UITextBox" is for a creating a TextBox object"
     *    </li><li>"UIButton" is for a creating a UIButton object" 
     *    </li>
     * </ul>
     * @param params  Construction parameters of UIObject
     * <ul>
     *    <li>new Object[]{"idOfTextBox","valueToTypeToTextBox"}
     *    </li>
     *    <li>new Object[]{"idOfButton"}
     *    </li>
     * </ul>
     * @return returns an UIObject, if UIObject could not created returns null value 
     * @see WebAutomationDecorator 
     */
    public WebAutomationDecorator createUIObject(String objType,
                                                 Object[] params,
                                                 String msgId)
    {
        try {
            if (params == null || objType == null) {
                return null;
            }
            Class cl =
                Class.forName("org.testis.webautomationmodule.ui.objects." +
                              objType);
            if (cl == null) {
                return null;
            }
            Constructor[] constr = cl.getDeclaredConstructors();
            for (int i = 0; i < constr.length; i++) {
                Class[] constParams = constr[i].getParameterTypes();
                if (constParams.length == params.length) {
                    WebAutomationDecorator dec = (WebAutomationDecorator) constr[i].newInstance(
                        params);
                    dec.setMessageId(msgId);
                    return dec;
                }
            }
        } catch (InstantiationException ex) {
            LogSenderService.putMessageOnQueue("UIObjectCreator: {ObjectType:" +
                                               objType +
                                               " Params:" + params + "}",
                                               AtofLogLevel.ERROR,
                                               " could not instantiate object(InstantiationException)...",
                                               msgId);
        } catch (IllegalAccessException ex) {
            LogSenderService.putMessageOnQueue("UIObjectCreator: {ObjectType:" +
                                               objType +
                                               " Params:" + params + "}",
                                               AtofLogLevel.ERROR,
                                               " could not access to object(IllegalAccessException)...",
                                               msgId);
        } catch (IllegalArgumentException ex) {
            LogSenderService.putMessageOnQueue("UIObjectCreator: {ObjectType:" +
                                               objType +
                                               " Params:" + params + "}",
                                               AtofLogLevel.ERROR,
                                               " could not create  object(IllegalArgumentException)... ",
                                               msgId);
        } catch (InvocationTargetException ex) {
            LogSenderService.putMessageOnQueue("UIObjectCreator: {ObjectType:" +
                                               objType +
                                               " Params:" + params + "}",
                                               AtofLogLevel.ERROR,
                                               " wrong invocation target for object(InvocationTargetException)...",
                                               msgId);
        } catch (ClassNotFoundException ex) {
            LogSenderService.putMessageOnQueue("UIObjectCreator: {ObjectType:" +
                                               objType +
                                               " Params:" + params + "}",
                                               AtofLogLevel.ERROR,
                                               " could not found class to create  object(ClassNotFoundException)...",
                                               msgId);
        } catch (NullPointerException ex) {
            LogSenderService.putMessageOnQueue("UIObjectCreator: {ObjectType:" +
                                               objType +
                                               " Params:" + params + "}",
                                               AtofLogLevel.ERROR,
                                               " null pointer exception while creating the object(NullPointerException)... ",
                                               msgId);
        } catch (Exception ex) {
            LogSenderService.putMessageOnQueue("UIObjectCreator: {ObjectType:" +
                                               objType +
                                               " Params:" + params + "}",
                                               AtofLogLevel.ERROR,
                                               "  exception caught while creating the object(Exception)... ",
                                               msgId);
        }
        return null;
    }
}

